package com.web.controller;

import com.common.dto.PageQuery;
import com.common.vo.DeveloperVO;
import com.common.vo.PageResult;
import com.common.vo.Result;
import com.web.service.DeveloperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * @Description
 * @Author nomo
 * @Date 2024/10/25 15:24
 */
@RestController
@Slf4j
@Api(value = "开发者Controller", tags = { "开发者信息接口" })
public class DeveloperContoller {

    @Autowired
    DeveloperService developerService;

    @GetMapping("/page")
    @ApiOperation(value = "分页加载数据")
    public Result<PageResult> page(PageQuery pageQuery){
        log.info("分页查询：{}",pageQuery);
        PageResult pageResult = developerService.pageQuery(pageQuery);
        return Result.success(pageResult);
    }

    @GetMapping("/inquire")
    @ApiOperation(value = "加载指定地区的数据")
    public  Result<PageResult> query(@RequestParam(required = false) String areas){
        PageResult query = developerService.query(areas);
        return Result.success(query);
    }


    @GetMapping("/info")
    @ApiOperation(value = "根据名字获取开发者评估信息")
    public Result<DeveloperVO> getDeveloper(@RequestParam("username")  String username) {
        log.info("查找开发者信息：{}", username);
        DeveloperVO developer = developerService.getDeveloper(username);
        if (developer == null) {
            return Result.error("用户不存在");
        }
        return Result.success(developer);
    }


    //这个只是用来初始化数据库的
    @GetMapping("/load")
    public void loadDeveloperByCSV() {
        String path = "D:/_Project/1024-creative-festival/script/userList.csv";
        log.info("初始化数据库内容：{}",path);

        String line;
        int index = 0;
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            // 读取表头（列名）
            br.readLine(); // 忽略第一行

            // 读取数据行
            while ((line = br.readLine()) != null) {
                if(index == 10000){
                    break;
                }
                if(line.contains("[bot]")){
                    continue;
                }
                if(index>=5220){
                    DeveloperVO developer = developerService.getDeveloper(line);
                    if(developer == null){
                        continue;
                    }
                    System.out.println("name=====" + developer.getUserName() + "======");
                }
                System.out.println("当前数据量：=======" + index + "=======");
                index++;

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
